package jpb.hotspot;

import jpb.CommandArg;
import jpb.CoreJvmArgs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static jpb.Assert.notNull;

/**
 * {@link jpb.JvmArg} implementation for Hotspot supporting the JVM's system properties as a map of strings.
 *
 * @see jpb.JvmArg
 * @see jpb.Jvm#systemPropertiesArg()
 * @author Dariusz Kordonski
 */
final class SystemPropertiesArg extends CoreJvmArgs.AbstractSystemPropertiesArg {

    private static final String SYSTEM_PROP_FORMAT = "-D%s=%s";

    SystemPropertiesArg() {
        super(HotspotJvm.class);
    }

    @Override
    protected void validate(Map<String,String> systemProperties) {
        notNull("systemProperties", systemProperties);
    }

    /**
     * Creates a class path string by combining prefix '-cp ' with list
     * of absolute paths of provided files separated by platform-specific
     * path separator.
     *
     * @see java.io.File#pathSeparatorChar
     *
     * @param {@inheritDoc}
     * @return {@inheritDoc}
     */
    @Override
    protected CommandArg convert(Map<String,String> systemProperties) {
        List<String> sysPropsList = new ArrayList<String>(systemProperties.size());
        for (Map.Entry<String,String> sysPropEntry : systemProperties.entrySet()) {
            sysPropsList.add(String.format(SYSTEM_PROP_FORMAT, sysPropEntry.getKey(), sysPropEntry.getValue()));
        }
        return CommandArg.of(sysPropsList);
    }

}
